package com.hbxy.favorite.service.impl;

import com.hbxy.favorite.dao.CollectMapper;
import com.hbxy.favorite.dao.FavoritesMapper;
import com.hbxy.favorite.domain.*;
import com.hbxy.favorite.resault.ResponseBo;
import com.hbxy.favorite.service.FavoritesService;
import com.hbxy.favorite.utils.RegUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class FavoritesServiceImpl implements FavoritesService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    FavoritesMapper mapper;
    @Autowired
    CollectMapper collectMapper;


    /**
     * 获取收藏夹
     * @param user
     * @return
     */
    @Override
    public ResponseBo getFavorites(User user) {

        FavoritesExample example =new FavoritesExample();
        FavoritesExample.Criteria criteria =example.createCriteria();

        if(RegUtil.isNotNull(user.getId())){

            criteria.andUserIdEqualTo(user.getId());
            List<Favorites> list =mapper.selectByExample(example);
            return ResponseBo.ok(list);
        }
        return ResponseBo.error("用户id不能为空");
    }
    /**
     * 添加收藏夹
     * @param favorites
     * @return
     */
    @Override
    public ResponseBo addFavorites(Favorites favorites) {

        if(favorites!= null && RegUtil.isNotNull(favorites.getUserId())) {
            if(RegUtil.isNotNull(favorites.getName())){
                boolean i=false;
                FavoritesExample example =new FavoritesExample();
                FavoritesExample.Criteria criteria =example.createCriteria();
                criteria.andUserIdEqualTo(favorites.getUserId());
                List<Favorites> favoritesList = mapper.selectByExample(example);

                for(Favorites favorites1:favoritesList){
                    if(favorites.getName().equals(favorites1.getName())){
                        i=true;
                    }
                }
                if(i) {
                    return ResponseBo.error("此收藏夹已存在");
                }

                favorites.setCreateTime(new Date().getTime());
                favorites.setLastModifyTime(new Date().getTime());
                mapper.insert(favorites);
                return  ResponseBo.ok("创建成功");
            }else
                return ResponseBo.error("收藏夹名称不能为空");

        }
        return ResponseBo.error("id不能为空");
    }

    @Override
    public ResponseBo updateFavorites(Favorites favorites) {
        return null;
    }

    @Override
    public int deleteFavorites(Favorites favorites) {

        int flag=2;
        if(RegUtil.isNotNull(favorites.getId())){
            CollectExample example =new CollectExample();
            CollectExample.Criteria criteria =example.createCriteria();
            criteria.andFavoritesIdEqualTo(favorites.getId());
            List<Collect> collectList = collectMapper.selectByExample(example);
            for(Collect collect:collectList){
                collect.setIsDelete("1");
                collect.setFavoritesId(null);
                collectMapper.updateByPrimaryKey(collect);
            }
             flag = mapper.deleteByPrimaryKey(favorites.getId());
            return flag;
        }
        return flag;
    }
}
